/**
 * Created by Administrator on 2018/1/18.
 */

layui.use(['jquery', 'laydate','form','layer','laypage','table'], function(){
    var $ = layui.jquery,
        laydate = layui.laydate,
        layer = layui.layer,
        table = layui.table,
        form = layui.form;

    var show = "off";

    laydate.render({
        elem: '#start'//指定元素
    });

    laydate.render({
        elem: '#end' //指定元素

    });

    var under_config = {
        elem: '#user',
        url:"/admin/get_menu_list_data",
        method:'post',
        cellMinWidth:80,
        cols:  [[
            //标题栏
            {field: 'name', title: '路由URL',align: 'left'},
            {field: 'type', title: '类型',align: 'center'},
            {field: 'url', title: '链接',align: 'left'},
            {field: 'icon', title: '菜单图标',align: 'center'},
            {field: 'sort', title: '排序',align: 'center'},
            {field: 'is_display', title: '显示为菜单',align: 'center'},
            {field: 'description', title: '描述',align: 'left'},
            {field: 'rule_name', title: '规则名称',align: 'center'},
            {field: 'created_at', title: '添加时间',align: 'center'},
            {fixed: 'right', align:'center', title: '操作',toolbar: '#barDemo'}
        ]],
        //data:data.data,
        // page: true,
        // limit: 15,
        // height: 680,
        size:'sm',
        done: function(res, curr, count){
            layer.closeAll();
            // $('#int_id').html(res.extra.user_option);
            form.render('select');
        }
    };

    //监听表格工具条
    table.on('tool(user)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'edit'){ //查看

            var id = data.id;
            layer.open({
                type: 2,
                area: ['700px', '600px'],
                fixed: false, //不固定
                maxmin: true,
                content: '/admin/menu_edit?id='+id
            });

        }else if(layEvent === 'del'){
            var id = data.id;
            //ajax执行
            $.ajax({
                type: "post",
                url: '/admin/menu_del',
                data:{
                    id:id,
                },
                dataType: "json",
                success: function(data){
                    if(data.code==200){
                        //关闭当前iframe页面
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        ins1.reload();
                        parent.layer.msg('菜单删除成功',{icon:1});
                    }else{
                        parent.layer.msg('菜单删除失败',{icon:2});
                        ins1.reload();
                    }
                }
            });
        }
    });

    //监听开关
    form.on('switch(switch)', function(data){
        if(this.checked==true){
            show = 'on';
        }else{
            show = 'off';
        }
    });

    //方法级渲染-默认显示入金记录
    var ins1 = table.render(under_config);

    //表格搜索和重载
    var $ = layui.$,
        active = {
            reload: function(){
                var index = layer.msg('数据加载中,请耐心等待...', {
                    icon: 16,shade: 0.5,time:15000
                });

                var int_id = $('#int_id').val();
                var start_time = $('#start').val();
                var end_time = $('#end').val();
                var keyword = $('#keyword').val();

                ins1.reload({
                    where: {
                        //根据MT4搜索
                        int_id:int_id,
                        status:status,
                        show:show,
                        keyword:keyword,
                        start_time:start_time,
                        end_time:end_time
                    }
                });
            },
            menu_add:function(){

                layer.open({
                    type: 2,
                    area: ['700px', '600px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/admin/menu_add'
                });

            }
        };

    $('.container .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

});
